<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="zlqblxx" label-width="100px" >
				<uni-section title="病理信息" type="line"></uni-section>
				<uni-forms-item label="活检方式" name="hjfs">
					<uni-data-select
					      :disabled="isDisabled" v-model="zlqblxx.hjfs"
					      :localdata="hjfsDictList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="淋巴结活检" name="lbjhj">
					<uni-data-checkbox :disabled="isDisabled" v-model="zlqblxx.lbjhj" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="病理学类型" name="blxlx">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.blxlx"  @change="autoSaven" placeholder="请输入病理学类型" />
				</uni-forms-item>
				<uni-forms-item label="组织学评分" name="zzxfj">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.zzxfj"  @change="autoSaven" placeholder="请输入组织学评分" />
				</uni-forms-item>
				<uni-forms-item label="ER百分比" name="blEr">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.blEr"  @change="autoSaven" placeholder="请输入ER百分比" />
				</uni-forms-item>
				<uni-forms-item label="PR百分比" name="blPr">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.blPr"  @change="autoSaven" placeholder="请输入PR百分比" />
				</uni-forms-item>
				<uni-forms-item label="Ki-67百分比" name="blKi">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.blKi"  @change="autoSaven" placeholder="请输入Ki-67百分比" />
				</uni-forms-item>
				<uni-forms-item label="Her2" name="blHer">
					<uni-data-select
					      :disabled="isDisabled" v-model="zlqblxx.blHer"
					      :localdata="her2DictList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="FISH" name="blFish">
					<uni-data-select
					      :disabled="isDisabled" v-model="zlqblxx.blFish"
					      :localdata="fishDictList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="分子分型" name="fzfx">
					<uni-data-select
					      :disabled="isDisabled" v-model="zlqblxx.fzfx"
					      :localdata="fzfxDictList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="是否基因检测" name="sfJyjc">
					<uni-data-checkbox :disabled="isDisabled" v-model="zlqblxx.sfJyjc" @change="ycxsChange($event,'jyjcjgShow','jyjcjg')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="基因检测结果" v-show="jyjcjgShow" name="jyjcjg">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.jyjcjg"  @change="autoSaven" placeholder="请输入基因检测结果" />
				</uni-forms-item>
				
				<uni-section title="术前治疗方案" type="line"></uni-section>
				<uni-forms-item label="新辅助治疗" name="faXfzzl">
					<uni-data-checkbox :disabled="isDisabled" v-model="zlqblxx.faXfzzl" @change="ycxsChange($event,'faFaShow','faFa')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="方案" v-show="faFaShow" name="faFa">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.faFa"  @change="autoSaven" placeholder="请输入方案" />
				</uni-forms-item>
				
				<uni-forms-item label="是否OFS" name="faSfOfs">
					<uni-data-checkbox :disabled="isDisabled" v-model="zlqblxx.faSfOfs" @change="ycxsChange($event,'faOfsYwShow','faOfsYw')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="OFS药物" v-show="faOfsYwShow" name="faOfsYw">
					<uni-easyinput :disabled="isDisabled" v-model="zlqblxx.faOfsYw"  @change="autoSaven" placeholder="请输入OFS药物" />
				</uni-forms-item>
				
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" autoHeight :disabled="isDisabled" v-model="zlqblxx.remark"  @change="autoSaven" placeholder="请输入备注"/>
				</uni-forms-item>
				
				
				<button type="primary" :disabled="isDisabled" @click="submit">保存</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
import { updateZlqblxx,getDicts,sfkybjData } from "@/api/trms/trms"
import { getZlqblxx } from "@/api/trms/query"
export default {
	data() {
		return {
			patientId: null,
			isDisabled: false,
			userId: null,
			hjfsDictList: [],
			fishDictList: [],
			fzfxDictList: [],
			her2DictList: [],
			jyjcjgShow: false,
			faFaShow: false,
			faOfsYwShow: false,
			dictSf: [
				{
				  text: '是',
				  value: 1
				}, {
				  text: '否',
				  value: 0
				}
			],
			// 表单数据
			zlqblxx: {},
			rules: {
				admissionNumber: {
				  rules: [{
					required: true,
					errorMessage: '住院号不能为空'
				  }]
				}
			}
		}
	},
	directives: {
	  limitInputNumber: {
		bind(el) {
		  el.oninput = () => {
			el.children[0].value = el.children[0].value.replace(/[^\d.]/g, '')
		  }
		}
	  }
	},
	onLoad: function(option) {
		this.getHjfsDict();
		this.getFishDict();
		this.getFzfxDict();
		this.getHer2DictList();
		this.patientId = option.patientId;
		this.userId = option.userId;
		sfkybjData(this);
		console.log("进入基本信息页面后、参数：",this.patientId)
		if(this.patientId){
			// 查询详情
			this.getDataByPatientId();
		}
	},
	onReady() {
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		/**
		 * 获取数据
		 */
		getDataByPatientId(){
			getZlqblxx(this.patientId).then(response => {
			  this.zlqblxx = response.data;
			  this.zlqblxx.hjfs = this.zlqblxx.hjfs + '';
			  this.zlqblxx.blFish = this.zlqblxx.blFish + '';
			  this.zlqblxx.blHer = this.zlqblxx.blHer + '';
			  this.zlqblxx.fzfx = this.zlqblxx.fzfx + '';
			  this.showAndHide(response.data);
			});
		},
		/**
		 * 修改
		 */
		submit(){
			this.$refs.form.validate().then(res => {
				updateZlqblxx(this.zlqblxx).then(response => {
				  if(response.code == 200){
					  this.$modal.msgSuccess("修改成功");
					  this.backWork();
					  return;
				  }
				  this.$modal.msgError(response.msg);
				});
			})
		},
		autoSaven(){
			this.$refs.form.validate().then(res => {
				updateZlqblxx(this.zlqblxx).then(response => {
				  console.log("修改成功");
				});
			})
		},
		/**
		 * 回到工作页面
		 */
		backWork(){
			setTimeout(function (){
				uni.navigateBack({
					delta: 1
				});
			},500);
		},
		/**
		 * 获取活检方式字典
		 */
		getHjfsDict(){
			getDicts("trms_hjfs").then(response => {
				this.hjfsDictList = this.dictFormat(response.data);
			})
		},
		getFishDict(){
			getDicts("trms_fish").then(response => {
				this.fishDictList = this.dictFormat(response.data);
			})
		},
		getFzfxDict(){
			getDicts("trms_fzfx").then(response => {
				this.fzfxDictList = this.dictFormat(response.data);
			})
		},
		getHer2DictList(){
			getDicts("trms_her2").then(response => {
				this.her2DictList = this.dictFormat(response.data);
			})
		},
		/**
		 * 给字典格式化成uniapp插件需要的格式
		 * @param {Object} list
		 */
		dictFormat(list){
			let newArr = [];
			if(!list){
				return newArr;
			}
			list.forEach(d=>{
				newArr.push({value: d.dictValue,text: d.dictLabel})
			})
			return newArr;
		},
		ycxsChange(e,show,valId){
			console.log(valId)
			const val = e.detail.value;
			this[show] = val == 1;
			if(Number(val) == 0 ){
				// 清理值
				this.zlqblxx[valId] = null;
			}
		},
		/**
		 * 隐藏或者显示
		 * @param {Object} jbxx 
		 */
		showAndHide(jbxx){
			this.jyjcjgShow = jbxx.sfJyjc == 1;
			this.faFaShow = jbxx.faXfzzl == 1;
			this.faOfsYwShow = jbxx.faSfOfs == 1;
		}
	}
}
</script>


<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
</style>
